<?php
$db = new MySQL();
$table = "banner";
$where = "";

//echo $where; die();
$paging = new paging();
$mod = $_GET['mod'];
$curpg = $_GET['curpg'];
$curpg = (isset($curpg)) ? $curpg : 1;
$gotopage = '?mod=' . $mod . "&act=list&idcat=" . $idcat;

$perpage = 99;


$query = "select * from $table $where order by orderId";
$sql = $db->select($query);
$totalrows = $db->numrows($sql);

$query = "select * from $table $where order by orderId limit " . (($curpg - 1) * $perpage) . ", " . $perpage;
$sql = $db->select($query);
?>


<script type="text/javascript">
    function add() {
        window.location.href = "?mod=<?= $mod ?>&act=numadd";
    }

    function edit(id) {
        var frm = document.frmlist;
        frm.listid.value = id + ",";
        var listid = frm.listid.value;
        window.location.href = "?mod=<?= $mod ?>&act=editlist&listid=" + listid;
    }

    function editlist() {
        var frm = document.frmlist;
        if (<?= $totalrows ?> != 0)
            frm.listid.value = selectid(frm.chkid);
        if (frm.listid.value == "") {
            if (<?= $totalrows ?> != 0)
                alert("<?= $msg_ask_select ?>");
        }
        else {
            var listid = frm.listid.value;
            window.location.href = "?mod=<?= $mod ?>&act=editlist&listid=" + listid;
        }
    }

    function del(id) {
        var frm = document.frmlist;
        bok = confirm("<?= $msg_deleteone ?>");
        if (bok) {
            frm.listid.value = id + ",";
            frm.action = "?mod=<?= $mod ?>&act=process";
            frm.submit();
        }
    }

    function delall(id) {
        var frm = document.frmlist;
        if (<?= $totalrows ?> != 0)
            frm.listid.value = selectid(frm.chkid);
        if (frm.listid.value == "") {
            if (<?= $totalrows ?> != 0)
                alert("<?= $msg_ask_select ?>");
        }
        else {
            bok = confirm("<?= $msg_conf_del ?>");
            if (bok) {
                frm.action = "?mod=<?= $mod ?>&act=process";
                frm.submit();
            }
        }
    }

    function submitfrm(idcat) {
        window.location.href = "?mod=<?= $mod ?>&act=list&idcat=" + idcat;
    }
</script>

<script type="text/javascript" src="scriptaculous/prototype.js"></script>  
<script type="text/javascript" src="scriptaculous/scriptaculous.js"></script>  
<script type="text/javascript">
//<![CDATA[  
    document.observe('dom:loaded', function() {
        var changeEffect;
        Sortable.create('sortlist', {tag: 'li', overlap: 'horizontal', constraint: false,
            onChange: function(item) {
                var list = Sortable.options(item).element;
                //$('changeNotification').update(Sortable.serialize(list).escapeHTML());  
                if (changeEffect)
                    changeEffect.cancel();
                changeEffect = new Effect.Highlight('changeNotification', {restoreColor: "transparent"});
            },
            onUpdate: function() {
                new Ajax.Request("<?= $mod ?>_order.php", {
                    method: "post",
                    onLoading: function() {
                        $('activityIndicator').show()
                    },
                    onLoaded: function() {
                        $('activityIndicator').hide()
                    },
                    parameters: {data: Sortable.serialize("sortlist")}
                });
            }
        });
    });
// ]]>  
</script>  
<style>
    li { list-style:none; cursor:move; float:left; }	
    li .img { width:150px; height:50px; overflow:hidden; margin:8px;}
</style>	

<div class="page-header">
    <div class="pull-left">Banner</div>
    <div class="pull-right"></div>
    <div class="clear"></div>
</div>

<form name="frmlist" method="post" action="">	
    <div class="pull-left">
        <a href="javascript:docheckallnew(false, document.frmlist.chkid)" >Deselect All</a> | 
        <a href="javascript:docheckallnew(true, document.frmlist.chkid)" >Select All</a>
    </div>
    <div class="pull-right">
        <img src="images/icons/add.png" onclick="add();" class="pointer"/> 
        <img src="images/icons/editbtn.png" onclick="editlist();" class="pointer" /> 
        <img src="images/icons/delete.png" onclick="delall();" class="pointer"/> 
    </div>
    <div class="clear"/>
    <table width="100%" border="0" cellpadding="0" cellspacing="1" class="gridtitle">
        <tr>
            <td class="gridtitletitle">&nbsp; Banner</td>
        </tr>

        <tr class="<?= $listitem ?>" onMouseOver="mOvr(this, 'bgover')" onMouseOut="mOut(this, '<?= $listitem ?>')">
            <td>

                <div id="changeNotificationNo"> </div>  
                <div id="sortlist" style="margin:auto;">
                    <?php
                    if ($totalrows != 0) {
                        $i = 1;
                        while ($row = $db->fetch($sql)) {
                            ?>
                            <li  id="pictureId_<?= $row['id'] ?>">
                                <div class="img"><img src="<?= APPLICATION_URL ?>banner-img/<?= $row['image'] ?>"  width="150px" /></div> 
                                <input type="checkbox" name="chkid" value="<?= $row["id"] ?>" />
                                <img src="images/icons/edit.png" onclick="edit('<?= $row["id"] ?>')" class="pointer"/>&nbsp;&nbsp;&nbsp;<img src="images/icons/del.png" onclick="del('<?= $row["id"] ?>')" class="pointer" />
                            </li>	

                            <?php
                            $i++;
                        }
                    }
                    ?>	
                </div>											</td>	
        </tr>

    </table>
    <table width="100%" border="0" cellspacing="0">
        <tr>
            <td width="68%" align="left" valign="middle" class="titlepage"><?= $lb_tottalrow ?>  <span class="numpage"><?= $totalrows ?></span> </td>
            <td width="32%" align="right" valign="top" class="numpage"><? echo $paging->pagenew($totalrows, $perpage, $pagecount, $curpg, $gotopage, "","titlepage");?></td>
        </tr>
    </table>


    <input type="hidden" name="form_" value="delete" />		
    <input type="hidden" name="listid" value="" />
    <input type="hidden" name="curpg" value="<?= $curpg ?>" />
</form>	

<?php
$db->close();
?>
